#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
#define x first
#define y second
const int N = 1e3 + 10, M = 1e5 + 10;
LL dis1[N], dis2[N];
int a1[M], b1[M], c1[M];
int h[N], e[M], ne[M], idx, w[M];
int st[N];
int n, m;
void add(int a, int b,int c)
{
	e[idx] = b, w[idx] = c; ne[idx] = h[a], h[a] = idx++;
}

void dij1(int s)
{
	priority_queue<PII, vector<PII>, greater<PII>>heap;
	//memset(dis1, 0x3f, sizeof(dis1));
	for (int i = 1; i <= n; i++) dis1[i] = 1e15;
	dis1[s] = 0;
	heap.push({ 0,s });
	while (heap.size()) {
		PII t = heap.top();
		heap.pop();
		int ver = t.y;
	    LL distance = t.x;
		if (st[ver])continue;
		st[ver] = 1;
		for (int i = h[ver]; ~i; i = ne[i]) {
			int j = e[i];
			if (dis1[j] > distance + w[i]) {
				dis1[j] = distance + w[i];
				heap.push({ dis1[j],j });
			}
		}
	}
}
void dij2(int s)
{
	priority_queue<PII, vector<PII>, greater<PII>>heap;
	//memset(dis2, 0x3f, sizeof(dis2));
	for (int i = 1; i <= n; i++)dis2[i] = 1e15;
	dis2[s] = 0;
	heap.push({ 0,s });
	while (heap.size()) {
		PII t = heap.top();
		heap.pop();
		int ver = t.y;
		LL distance = t.x;
		if (st[ver])continue;
		st[ver] = 1;
		for (int i = h[ver]; ~i; i = ne[i]) {
			int j = e[i];
			if (dis2[j] > distance + w[i]) {
				dis2[j] = distance + w[i];
				heap.push({ dis2[j],j });
			}
		}
	}
}
int main()
{
	cin >> n >> m;
	memset(h, -1, sizeof(h));
	for (int i = 1; i <= m; i++) {
		cin >> a1[i] >> b1[i] >> c1[i];
		add(a1[i], b1[i], c1[i]);
	}
	dij1(1);
	/*for (int i = 1; i <= n; i++) {
		cout << "dis[i]==" << dis1[i] << endl;
	}*/
	memset(h, -1, sizeof(h));
	memset(ne, 0, sizeof(ne));
	memset(e, 0, sizeof(e));
	memset(w, 0, sizeof(w));
	memset(st, 0, sizeof(st));
	idx = 0;
	for (int i = 1; i <= m; i++) {
		add(b1[i], a1[i], c1[i]);
	}
	dij2(1);
	LL ans = 0;
	for (int i = 1; i <= n; i++) {
		ans += dis1[i] + dis2[i];
	}
	cout << ans << endl;
	return 0;
}